package com.gcmm;

import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;

public class GlobalKeyListener implements NativeKeyListener {
    public StringBuilder key = new StringBuilder();

    public void nativeKeyPressed(NativeKeyEvent e) {
        try {
            //获取输入字符
            String keyText = NativeKeyEvent.getKeyText(e.getKeyCode());
            //判断是否为Ctrl键
            if ("Ctrl".equals(keyText)) {
                key = new StringBuilder("Ctrl");
            } else {
                //如果不是Ctrl键，则将输入字符添加到key中
                key.append(keyText);
                //判断是否为Ctrl组合键
                switch (key.toString()) {
                    // Ctrl+Q组合键
                    // 开始/关闭鼠标连点
                    case "CtrlQ":
                        System.out.println("CtrlQ pressed");
                        if (Mouse.changeMouseState())
                            Mouse.continueClickAsync();
                        break;

                    // Ctrl+W组合键
                    // 退出后台，结束程序
                    case "CtrlW":
                        System.out.println("CtrlW pressed");
                        System.exit(0);
                        break;
                }

            }
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }

    }

    public void nativeKeyReleased(NativeKeyEvent e) {
        System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
    }

    public void nativeKeyTyped(NativeKeyEvent e) {
        System.out.println("Key Typed: " + e.getKeyText(e.getKeyCode()));
    }


}
